using System;
using System.Collections.Generic;
using System.Text;
using ACS.Compiler.Tables;
using ACS.Compiler.Tables.Modes;
using ACS.Compiler.Tree.Interfaces;
using ACS.Compiler.Util;

namespace ACS.Compiler.Tree.Impl
{
    public class Ident : Expression, IFinalIdent
    {

        public string Name
        {
            get { return ToString(); }
        }

        public override Expressions ExpressionType
        {
            get { return Expressions.Ident; }
        }

        public override Mode Mode
        {
            get
            {
                 throw new NotImplementedException();
            }
        }

        public override void Resolve(string ns, DataTable localData)
        {
            throw new NotImplementedException();
        }

        public override string ToDeclareString()
        {
            return Name;
        }

        public Mode ResolveIdent(string ns, DataTable localData, Mode parentMode)
        {
            if (parentMode == null)
            {
                DataField ret = DataTable.Instance.FindIdent(ns, Name);
                if (ret == null && localData !=null)
                {
                    ret = localData.FindIdent(null, Name);
                }
                if (ret == null)
                {
                    Logger.Instance.TraceError("ident not found " + Name, this);
                    return null;
                }
                return ret.Mode;
            } else
            {
                ContainerMode parentContainer = parentMode as ContainerMode;
                if (parentContainer != null )
                {
                    return _mode = parentContainer.ResolveField(Name, this);
                } else
                {
                    Logger.Instance.TraceError("Wrong container type!", this);
                }
            }
            return null;
        }
    }
}
